var utilService = require('../../utilService.js')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showHome: {
      type: Number,
      value: 1,
      observer: function (newValue, oldValue) {
        var i = this.data.count + newValue - 1
        this.setData({count : i})
      }
    },

    showService: {
      type: Number,
      value: 0,
      observer: function (newValue, oldValue) {
        var i = this.data.count + newValue
        this.setData({ count: i })
      }
    },

    showMsg: {
      type: Number,
      value: 0,
      observer: function (newValue, oldValue) {
        var i = this.data.count + newValue
        this.setData({ count: i })
        if (newValue == 1) {
          this.requestMsgNum()
        }
      }
    }, 
    newMsg: {
      type: Number,
      value: 0,
      observer: function (newValue, oldValue) {
        this.setData({msgNum: newValue+''})
      }
    },
    bottom: {
      type: Number,
      value: 70
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showBackBool: false,
    count: 1,
    showNewMsg: 0,
    msgNum:''
  },

  ready: function () {
    console.log(this)
  },

  /**
   * 组件的方法列表
   */
  methods: {
    requestMsgNum: function () {
      let self = this
      utilService.requestMessage(function (count) {
        self.setData({ msgNum: count })
      },
      function (error) {

      })
    },

    showBack(action) {
      if (this.data.showBackBool == true) {
        return
      }

      var count = this.data.count + 1
      this.setData({
        showBackBool: true,
        count: count
      })
      this.action = action
    },

    dismissBack() {
      if (this.data.showBackBool == false) {
        return
      }

      var count = this.data.count - 1
      this.setData({
        showBackBool: false,
        count: count
      })
    },

    gotoTop() {
      this.dismissBack()
      typeof this.action == "function" && this.action()
    },
    toMsg() {
      wx.switchTab({
        url: '/pages/mine/message/message'
      })
    },
    toHome() {
      //判断扩返回就返回 不可以返回reLaunch
      let arr = getCurrentPages()
      for (var i=0;i<arr.length;i++){
        var item = arr[i];
        if (item.route == "pages/home/home"){
            wx.navigateBack({
              url: '/pages/home/home',
            })
            return;
        }
      }
      wx.reLaunch({
        url: '/pages/home/home',
      })
    },
  }
})
